﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FAQFrameworks
{
    public class MessageUtil
    {
        const string msgKey = "Message";

        public static void SetMessage(string msg)
        {
            HttpResponse response
                = HttpContext.Current.Response;

            HttpCookie cookie = new HttpCookie(msgKey);
            cookie.Value = HttpUtility.UrlEncode(msg);

            cookie.Expires = DateTime.Now.AddDays(1);

            response.Cookies.Add(cookie);
        }

        public static string GetMessage()
        {
            HttpRequest request
                = HttpContext.Current.Request;

            HttpCookie cookie = request.Cookies[msgKey];
            if (cookie != null)
            {
                string msg = HttpUtility.UrlDecode(cookie.Value);

                HttpResponse response
                = HttpContext.Current.Response;

                cookie = new HttpCookie(msgKey);

                cookie.Expires = DateTime.Now.AddDays(-1);

                response.Cookies.Add(cookie);

                return msg;
            }

            return string.Empty;
        }
    }
}
